package com.zhiwang.planet.mvp.p

import android.graphics.BitmapFactory
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.GraphView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.util.androidSchedule
import javax.inject.Inject

@ActivityScope
class GraphCodePresenter @Inject constructor(var restApi: RestApi) {

    lateinit var graphView: GraphView

    fun getRegisterGraphCode(random: String) {
//        val graphCodeRequest = GraphRequest(random)
        var disposable = restApi.getGraphCode(random, "app_reg")
                .androidSchedule()
                .subscribe({
                    val bitmap = BitmapFactory.decodeStream(it.byteStream())
                    graphView.onGraphCodeLoad(bitmap)
                }, {
                    graphView.onError(it)
                }, {

                })


    }

    fun getFindGraphCode(seed: String) {
        var disposable = restApi.getGraphCode(seed, "app_findpwd")
                .androidSchedule()
                .subscribe {
                    val bitmap = BitmapFactory.decodeStream(it.byteStream())
                    graphView.onGraphCodeLoad(bitmap)
                }
    }

    fun getChangeGraphCode(seed: String) {
        var disposable = restApi.getGraphCode(seed, "app_changepwd")
                .androidSchedule()
                .subscribe {
                    val bitmap = BitmapFactory.decodeStream(it.byteStream())
                    graphView.onGraphCodeLoad(bitmap)
                }
    }

    fun getBindGraphCode(seed: String) {
        var subscribe = restApi.getGraphCode(seed, "app_bind")
                .androidSchedule()
                .subscribe {
                    val bitmap = BitmapFactory.decodeStream(it.byteStream())
                    graphView.onGraphCodeLoad(bitmap)
                }
    }

//    fun getPayPasswordGraphCode(seed:String){
//        var subscribe = restApi.getGraphCode(seed, "app_bind")
//                .androidSchedule()
//                .subscribe {
//                    val bitmap = BitmapFactory.decodeStream(it.byteStream())
//                    graphView.onGraphCodeLoad(bitmap)
//                }
//    }

}